package com.yiyang.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.domain.ResultPage;
import com.yiyang.domain.po.NurseContent;
import com.yiyang.domain.po.NurseLevelItem;
import com.yiyang.mapper.NurseLevelItemMapper;
import com.yiyang.service.NurseContentService;
import com.yiyang.mapper.NurseContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author itcast
* @description 针对表【nurse_content】的数据库操作Service实现
* @createDate 2024-12-23 15:32:15
*/
@Service
public class NurseContentServiceImpl extends ServiceImpl<NurseContentMapper, NurseContent>
    implements NurseContentService{

    @Autowired
    private NurseContentMapper nurseContentMapper;

    @Override
    public ResultPage<NurseContent> findNurseItemPage(Integer page, Integer pageSize, String itemName, Integer levelId, Integer status) {
        // 组装分页条件
        Page<NurseContent> nurseContentPage = new Page<>(page,pageSize);
        // 组装查询条件
        // select * from nurse_content where nursing_name like '%氧%' and status = 1 and is_deleted=0 and id not in(select item_id from nurse_level_item where level_id=1)
        LambdaQueryWrapper<NurseContent> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(itemName),NurseContent::getNursingName,itemName)
                .eq(status != null,NurseContent::getStatus,status)
                .eq(NurseContent::getIsDeleted,0)
                .notInSql(levelId != null,NurseContent::getId,"select item_id from nurse_level_item where level_id="+levelId);

        nurseContentMapper.selectPage(nurseContentPage,lqw);

        return new ResultPage<>(nurseContentPage.getTotal(),nurseContentPage.getRecords());
    }



    @Override
    public List<NurseContent> listNurseItemByLevel(Integer levelId) {

        // 组装查询条件
        // select * from nurse_content where id in(select item_id from nurse_level_item where level_id = 1);
        LambdaQueryWrapper<NurseContent> lqw = new LambdaQueryWrapper<>();
        lqw.inSql(levelId != null,NurseContent::getId,"select item_id from nurse_level_item where level_id ="+levelId);

        List<NurseContent> nurseContentList = nurseContentMapper.selectList(lqw);
        return nurseContentList;
    }

    @Autowired
    private NurseLevelItemMapper nurseLevelItemMapper;

    @Override
    public void updateNurseItem(NurseContent nurseContent) {
        // 判断是否修改状态为停用
        if(nurseContent.getStatus() == 2){
            LambdaQueryWrapper<NurseLevelItem> lqw = new LambdaQueryWrapper<>();

            lqw.eq(nurseContent.getId()!=null , NurseLevelItem::getItemId,nurseContent.getId());
            Long count = nurseLevelItemMapper.selectCount(lqw);

            if(count > 0){
                nurseLevelItemMapper.delete(lqw);
            }
        }

        // 修改护理项目
        nurseContentMapper.updateById(nurseContent);
    }

    @Override
    public void delNurseItem(Integer id) {

        LambdaQueryWrapper<NurseLevelItem> lqw = new LambdaQueryWrapper<>();

        lqw.eq(id!=null , NurseLevelItem::getItemId,id);
        Long count = nurseLevelItemMapper.selectCount(lqw);

        if(count > 0){
            nurseLevelItemMapper.delete(lqw);
        }

        // 逻辑删除
        NurseContent nurseContent = new NurseContent();
        nurseContent.setId(id);
        nurseContent.setIsDeleted(1);
        nurseContentMapper.updateById(nurseContent);
    }
}




